package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.GraphInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;


    //根据id获取公寓信息
    @Override
    public ApartmentDetailVo listApartmentDetailVoById(Long id) {
        //获取公寓基本信息
       ApartmentInfo apartmentInfo =apartmentInfoMapper.selectById(id);
        //获取标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.listLableInfoListMapper(id);
        //获取图片信息
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphVOByApartmentIdAndItemTypeId(id,ItemType.APARTMENT);
        //3.根据公寓id查询公寓配套
        List<FacilityInfo> facilityInfoList =  facilityInfoMapper.selectFacilityByApartmentId(id);
        //获取公寓最低房租
        BigDecimal minRent = roomInfoMapper.getApartmentInfoMinRentList(id);

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        //设置是否删除
        apartmentDetailVo.setIsDelete(apartmentInfo.getIsDeleted()==1);

        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setMinRent(minRent);
        return apartmentDetailVo;
    }


    //根据房间id获取所属公寓详细信息
    @Override
    public ApartmentItemVo getApartmentItemVoListByRoomId(Long apartmentId) {
        //根据公寓id获取公寓的基本信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.getApartmentItemVoByRoomId(apartmentId);
        //获取公寓标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.listLableInfoListMapper(apartmentId);
        //获取图片信息
        List<GraphInfo> graphInfoList = graphInfoMapper.selectGraphInfoByApartmentIdAndItemTypeId(apartmentId, ItemType.ROOM);
        //获取最小租金
        BigDecimal minRentList = roomInfoMapper.getApartmentInfoMinRentList(apartmentId);

        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentItemVo);

        apartmentItemVo.setLabelInfoList(labelInfoList);
        apartmentItemVo.setGraphVoList(graphInfoList);
        apartmentItemVo.setMinRent(minRentList);

        return apartmentItemVo;
    }

    //根据公寓id查询房间列表
    @Override
    public Page<RoomItemVo> pageRoomItemVoListByApartmentId(Page<RoomItemVo> roomItemVoPage, Long id) {

        return baseMapper.pageRoomItemVoListByApartmentId(roomItemVoPage,id);
    }
    //根据id查询公寓基本信息
    @Override
    public ApartmentItemVo listApartmentItemVoById(Long id) {
        //获取公寓基本信息
        ApartmentInfo apartmentInfo =apartmentInfoMapper.selectById(id);
        //获取标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.listLableInfoListMapper(id);
        //获取图片信息
        List<GraphInfo> graphVoList = graphInfoMapper.selectGraphInfoByApartmentIdAndItemTypeId(id,ItemType.APARTMENT);
        //获取公寓最低房租
        BigDecimal minRent = roomInfoMapper.getApartmentInfoMinRentList(id);

        //设置
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentItemVo);
        apartmentItemVo.setLabelInfoList(labelInfoList);
        apartmentItemVo.setGraphVoList(graphVoList);
        apartmentItemVo.setMinRent(minRent);

        return apartmentItemVo;
    }
}




